*final update 2025.02.03



********************************************************************************
//STEP 01: IMPORT DATA
********************************************************************************
* Change Directory to the Alpha_Do_File .do file's location
local 	home_folder "/Users/demattee/Desktop/JLC_Replication"
cd 		"`home_folder'"
use 	"`home_folder'/Data/JLC_synth_base_city_week.dta", clear



********************************************************************************
// STEP 02: DROP MISSING DATA
********************************************************************************
//Drop hearing_base_city codes that ruin perfectly balanced panel. Verify this with each new version of the data

//A) Create Time Variable Across Cities
keep if time <=202011
sort 	hearing_base_city time
bysort 	hearing_base_city: gen X = _N

//B) Use hearing_base_city=="BOS" as the benchmark for how many observations a unit must have to be in the donor pool
tab 	time hearing_base_city if X<52


//C) Boot all units that have fewer observatyions than BOS
keep if	X==52
drop	X
codebook hearing_base_city, compact

//D) Now assign IDs to the Cities 
sort 	hearing_base_city time    
bysort 	hearing_base_city: gen id_time = _n
sort 	hearing_base_city id_time
encode 	hearing_base_city, generate(id_city) label(id_city)
tab 	id_city if hearing_base_city=="BOS", nolab



********************************************************************************
// STEP 03: CREATE TIME ID & DROP MISSING DATA
********************************************************************************
//Declare dataset a panel
tsset 	id_city id_time
		order time hearing_base_city id_time id_city

		
		
********************************************************************************		
// STEP 04: CREATE LOCALS
********************************************************************************
/*Synthetic Control Quick Facts: These are the facts that execute the synth. 
These 'facts' change if we toy with the sample (observations or time), so 
setting a local keeps things simple (i.e., only on change)*/

//Identify the treatment unit
tab 		id_city if hearing_base_city=="BOS", nolab

//Identify how many jurisdictions are in the data
codebook 	id_city, compact

//Set Locals for synthetic control units
local MA		"5"					// What is MA's id_city? UNSURE?: tab id_city if hearing_base_city=="BOS", nolab
local units		"24"				// How many jurisdictions are there? UNSURE?: codebook 	id_city, compact

//Set Local for start dates
local begin		"1"					// The first id_time in the analysis
local MSPEstart "1"					// Period in which MSPE minimization begins 														
		
//Set Local for treatment dates
local pretreat	"38"				// What id_time corresponds with the Brito vs. Barr decision date
local decision	"39"				// Brito vs. Barr opinion published (Wednesday, Nov 27, 2019).
										// id_time	time	date_day
										// 37		201948	2019.11.27
									// court orders that immigration courts shall follow the requirements, effective Friday, December 13, 2019.
										// id_time	time	date_day
										// 39		201950	2019.12.13
local posttreat "40"				// What is the first id_time after the decision date? UNSURE: decision +1

//IDENTIFY THE ENDING DATES
local endtime	"52"				// id_time 53==2020 week 12 (Friday, March 20th) as the last id_time in the analysis?
local covid		"52"				// POTUS Declares COVID-19 a National Emergency (Friday, March 13).
										
//Set locals to ensure that all analyses run the same model
local outcome 	"idnassocbond"													 
local pretime 	"idnassocbond(8) idnassocbond(22) idnassocbond(24) idnassocbond(37)"				
local court		"trump_ij obama_ij  ij_experience"							
local case		"crime_charge_yes meeting_number"								
local econ		"log_state_gdp"												
local society	"log_state_pop state_govt_employees_capita"						



********************************************************************************		
// STEP 05: FINALIZE THE SYNTHETIC CONTROL
********************************************************************************	
//THIS RUNS A SDID
// gen 	intervention = 0
// replace intervention = 1 if hearing_base_city=="BOS" & id_time >=39
// sdid	idnassocbond id_city id_time intervention, vce(placebo) reps(100) seed(1213) covariates(`court' `case' `econ' `society') graph
/*
	Synthetic Difference-in-Differences Estimator

	-----------------------------------------------------------------------------
	idnassocbond |     ATT     Std. Err.     t      P>|t|    [95% Conf. Interval]
	-------------+---------------------------------------------------------------
	   treatment |  -1.88299   17.68090    -0.11    0.915   -36.53691    32.77094
	-----------------------------------------------------------------------------
	95% CIs and p-values are based on Large-Sample approximations.
	Refer to Arkhangelsky et al., (2020) for theoretical derivations.
*/

//MINIMIZE PRE-TREATMENTS RMSPE ONLY: idnassocbond
	foreach i of numlist `MA'   {	
	synth 		`outcome'																///
				`pretime' `court' `case' `politics' `econ' `society'  					///
				, trunit(`i') trperiod(`decision') 										///
				mspeperiod(`MSPEstart'(1)`pretreat')									/// 
				resultsperiod(`begin'(1)`endtime')										/// Results obtained for periods. This is how we change the years displayed in the figure
				fig nested // allopt   													// 
	}


//Now do a quick collapse so that we can do generate a graph showing how much better the synth is
preserve
	drop if		hearing_base_city=="BOS" 
	keep if		id_time <`decision'
	collapse 	(mean) sample_mean = idnassocbond `court' `case' `politics' `econ' `society', by(id_time)
	rename		id_time _time
	sum			`court' `case' `politics' `econ' `society'
	sum 		sample_mean if _time ==8 
	sum 		sample_mean if _time ==22 
	sum 		sample_mean if _time ==24
	sum 		sample_mean if _time ==37 
	save 		"`home_folder'/Data/Sample Means/sample_mean_caseload_weekly.dta", replace 
restore
	
*tell Stata where to save the .dta files it generates for RMSPE Ratio Calculations
cd "`home_folder'/Data/Placebo Results Caseload/Weekly"

//Create a temporary matrix (resmat) and save results of synth command
tempname resmat
foreach i of numlist `MA' {	 
	synth 	`outcome' 																///
			`pretime' `court' `case' `politics' `econ' `society'					///
			, trunit(`i') trperiod(`decision') 										///
			mspeperiod(`MSPEstart'(1)`pretreat')									/// Results obtained for periods. This is how we change the years displayed in the figure
			nested /// allopt   													/// 
			keep(caseload_synth_`i') replace
	matrix `resmat' = nullmat(`resmat') \ e(Y_treated)
	local names `"`names' `"`i'"'"'
	}



********************************************************************************		
// STEP 06: USE LOOPS TO CALCULATE TWO RMSPE FOR EACH UNIT (ONE PRE AND ONE POST)
********************************************************************************	
local 	home_folder "/Users/demattee/Desktop/JLC_Replication"
do 		"`home_folder'/Scripts/JLC_02b_Caseload_Weekly_RMSPE_ratio.do"



********************************************************************************		
// STEP 07: GENERATE GRAPHS
********************************************************************************
local 	home_folder "/Users/demattee/Desktop/JLC_Replication"
	
preserve
// 	Remove Boston becuase it's not part of the donor pool
	drop if		hearing_base_city=="BOS"
	collapse 	(mean) sample_mean = idnassocbond, by(id_time)
	rename		id_time _time
	merge 1:1 	_time using "`home_folder'/Data/Placebo Results Caseload/Weekly/caseload_synth_`MA'.dta", keepusing(_Y_synthetic _Y_treated)
	label define 	week_labels	1	"19-Mar"	///	2019.03.19
								2	"28-Mar"	///	2019.03.28
								3	"2-Apr"		///	2019.04.02
								4	"10-Apr"	///	2019.04.10
								5	"17-Apr"	///	2019.04.17
								6	"23-Apr"	///	2019.04.23
								7	"30-Apr"	///	2019.04.30
								8	"13-May"	///	2019.05.13
								9	"16-May"	///	2019.05.16
								10	"21-May"	///	2019.05.21
								11	"30-May"	///	2019.05.30
								12	"10-Jun"	///	2019.06.10
								13	"13-Jun"	///	2019.06.13
								14	"24-Jun"	///	2019.06.24
								15	"25-Jun"	///	2019.06.25
								16	"3-Jul"		///	2019.07.03
								17	"9-Jul"		///	2019.07.09
								18	"18-Jul"	///	2019.07.18
								19	"24-Jul"	///	2019.07.24
								20	"30-Jul"	///	2019.07.30
								21	"8-Aug"		///	2019.08.08
								22	"19-Aug"	///	2019.08.19
								23	"20-Aug"	///	2019.08.20
								24	"27-Aug"	///	2019.08.27
								25	"4-Sep"		///	2019.09.04
								26	"10-Sep"	///	2019.09.10
								27	"17-Sep"	///	2019.09.17
								28	"25-Sep"	///	2019.09.25
								29	"1-Oct"		///	2019.10.01
								30	"10-Oct"	///	2019.10.10
								31	"15-Oct"	///	2019.10.15
								32	"23-Oct"	///	2019.10.23
								33	"31-Oct"	///	2019.10.31
								34	"5-Nov"		///	2019.11.05
								35	"13-Nov"	///	2019.11.13
								36	"19-Nov"	///	2019.11.19
								37	"27-Nov"	///	2019.11.27
								38	"5-Dec"		///	2019.12.05
								39	"10-Dec"	///	2019.12.10
								40	"23-Dec"	///	2019.12.23
								41	"30-Dec"	///	2019.12.30
								42	"2-Jan"		///	2020.01.02
								43	"9-Jan"		///	2020.01.09
								44	"16-Jan"	///	2020.01.16
								45	"23-Jan"	///	2020.01.23
								46	"29-Jan"	///	2020.01.29
								47	"10-Feb"	///	2020.02.10
								48	"12-Feb"	///	2020.02.12
								49	"20-Feb"	///	2020.02.20
								50	"26-Feb"	///	2020.02.26
								51	"5-Mar"		///	2020.03.05
								52	"11-Mar"	///	2020.03.11
								, replace
	label 	values _time week_labels

//Graph comparing Boston Caseload to Donor Pool Only
	twoway 	(line _Y_treated _time if _time <= `decision', lcolor(gs4)  sort)			///
			///(line _Y_synthetic _time, lcolor(gs4) lpattern(longdash)) 				///
			(line sample_mean _time if _time <= `decision', lcolor(gs6) lpattern(shortdash)) 					///
			, title("Caseload", size(small)) 									///
				subtitle("") caption("") note("$S_DATE", size(tiny) span)				///
				xtitle("") 																///			
					xline(`decision', lwidth(medthick) lpattern(dot) lcolor(red))		///
					xlabel(`begin'(4)`endtime',  labsize(small) valuelabel) xmtick(##6) ///
				ytitle(Caseload, size(small)) 									///
					ylabel(20(20)100, labsize(small) grid glcolor(gs15%75)) ymtick(##3) ///
				graphregion(c(gs16)) 													///
				plotregion(ic(gs16)) 													///
				legend(on order(1 "Actual Boston" 2 "Base City Mean")					///
						cols(1) size(vsmall) nobox region(fcolor(none) lcolor(none)) 	///
						position(11) ring(0))											//
 				graph save "Graph" 	"`home_folder'/Graphs/bos_vs_pool_caseload_weekly.gph", replace	
 				graph export 		"`home_folder'/Graphs/bos_vs_pool_caseload_weekly.jpg", as(jpg) name("Graph") quality(100) replace

//Graph comparing Boston Caseload to Synthetic Only		
	twoway 	(line _Y_treated _time if _time <= `decision', lcolor(gs4)  sort)									///
			(line _Y_synthetic _time if _time <= `decision', lcolor(gs6) lpattern(longdash)) 					///
			///(line sample_mean _time, lcolor(gs4) lpattern(shortdash)) 				///
			, title("Caseload", size(small)) 									///
				subtitle("") caption("") note("$S_DATE", size(tiny) span)				///
				xtitle("") 																///			
					xline(`decision', lwidth(medthick) lpattern(dot) lcolor(red))		///
					xlabel(`begin'(4)`endtime',  labsize(small) valuelabel) xmtick(##6) ///
				ytitle(Caseload, size(small)) 									///
					ylabel(20(20)100, labsize(small) grid glcolor(gs15%75)) ymtick(##3) ///
				graphregion(c(gs16)) 													///
				plotregion(ic(gs16)) 													///
				legend(on order(1 "Actual Boston" 2 "Synthetic Boston")					///
						cols(1) size(vsmall) nobox region(fcolor(none) lcolor(none)) 	///
						position(11) ring(0))											//
 				graph save "Graph" 	"`home_folder'/Graphs/bos_vs_synth_caseload_weekly.gph", replace	
 				graph export 		"`home_folder'/Graphs/bos_vs_synth_caseload_weekly.jpg", as(jpg) name("Graph") quality(100) replace

//Graph comparing Boston Caseload to Donor Pool and Synthetic
	twoway 	(line _Y_treated _time if _time <= `decision', lcolor(gs4)  sort)									///
			(line sample_mean _time if _time <= `decision', lcolor(gs6) lpattern(shortdash)) 					///
			(line _Y_synthetic _time if _time <= `decision', lcolor(gs6) lpattern(longdash)) 					///
			, title("Caseload", size(small)) 									///
				subtitle("") caption("") note("$S_DATE", size(tiny) span)				///
				xtitle("") 																///			
					xline(`decision', lwidth(medthick) lpattern(dot) lcolor(red))		///
					xlabel(`begin'(4)`posttreat',  labsize(small) valuelabel) xmtick(##6) ///
				ytitle(Caseload, size(small)) 									///
					ylabel(20(20)100, labsize(small) grid glcolor(gs15%75)) ymtick(##3) ///
				graphregion(c(gs16)) 													///
				plotregion(ic(gs16)) 													///
				legend(on order(1 "Actual Boston" 2 "Base City Mean" 3 "Synthetic Boston")					///
						cols(1) size(vsmall) nobox region(fcolor(none) lcolor(none)) 	///
						position(11) ring(0))
			graph save "Graph" 	"`home_folder'/Graphs/bos_pool_synth_caseload_weekly.gph", replace
			graph export 		"`home_folder'/Graphs/bos_pool_synth_caseload_weekly.jpg", as(jpg) name("Graph") quality(100) replace	
restore


				
********************************************************************************
// STEP 08: RUN PLACEBO IN SPACE AND GENERATE TREATMENT EFFECT GRAPHS
********************************************************************************	
local 	home_folder "/Users/demattee/Desktop/JLC_Replication"
do 		"`home_folder'/Scripts/JLC_02c_caseload_Weekly_Placebos.do"


				
********************************************************************************
// STEP 09: FIND P-VALUES
********************************************************************************
local 	home_folder "/Users/demattee/Desktop/JLC_Replication"
do 		"`home_folder'/Scripts/JLC_02d_caseload_Weekly_Pvalues.do"
